package cn.iocoder.yudao.module.book.service.books;

//import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBooksRespVO;
import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBooksRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.book.controller.admin.books.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.books.BooksDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.book.dal.mysql.books.BooksMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.*;

/**
 * 绘本 Service 实现类
 *
 * @author xi
 */
@Service
@Validated
public class BooksServiceImpl implements BooksService {

    @Resource
    private BooksMapper sMapper;

    @Override
    public Integer creates(BooksSaveReqVO createReqVO) {
        // 自动计算排序：同一绘本集内按最大 sort + 1；若无 collectionId，则按 bookId 分组递增
        Integer resolvedSort = createReqVO.getSort();
        if (resolvedSort == null) {
            Integer base = 0;
            if (createReqVO.getCollectionId() != null) {
                base = sMapper.selectMaxSortByCollectionId(createReqVO.getCollectionId());
            } else if (createReqVO.getBookId() != null) {
                // 回退策略：若未提供 collectionId，则基于相同 bookId 的最大排序
                BooksDO probe = new BooksDO();
                probe.setBookId(createReqVO.getBookId());
                // 使用通用查询计算最大 sort
                List<BooksDO> list = sMapper.selectList(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<BooksDO>()
                        .select("MAX(sort) AS sort")
                        .eq("book_id", createReqVO.getBookId()));
                if (list != null && !list.isEmpty() && list.get(0) != null && list.get(0).getSort() != null) {
                    base = list.get(0).getSort();
                }
            }
            resolvedSort = (base == null ? 0 : base) + 1;
        }

        // 插入
        BooksDO s = BeanUtils.toBean(createReqVO, BooksDO.class);
        s.setSort(resolvedSort);
        sMapper.insert(s);
        // 返回
        return s.getBookId();
    }

    @Override
    public void updates(BooksSaveReqVO updateReqVO) {
        // 校验存在
        validatesExists(updateReqVO.getBookId());
        // 更新
        BooksDO updateObj = BeanUtils.toBean(updateReqVO, BooksDO.class);
        sMapper.updateById(updateObj);
    }

    @Override
    public void deletes(Integer id) {
        // 校验存在
        validatesExists(id);
        // 删除
        sMapper.deleteById(id);
    }

    private void validatesExists(Integer id) {
        if (sMapper.selectById(id) == null) {
            throw exception(S_NOT_EXISTS);
        }
    }

    @Override
    public BooksDO gets(Integer id) {
        return sMapper.selectById(id);
    }


    @Override
    public PageResult<BooksDO> getsPage(BooksPageReqVO pageReqVO) {
        return sMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppBooksRespVO> getByCollectionId(Integer id) {
        return sMapper.selectByCollectionsId(id);
    }

    @Override
    public AppBooksRespVO getByBookId(Integer bookId, String sort) {
        BooksDO bookDO=sMapper.getByBookId(bookId, sort);
        return BeanUtils.toBean(bookDO, AppBooksRespVO.class);
    }

    @Override
    public List<AppBooksRespVO> selectLikeContent(String content) {
        return sMapper.selectLikeContent(content);
    }


}